package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system._enum.KfnWorkOrderMaAdidasUpdateMethodEnum;
import com.ruoyi.system._enum.KfnWorkOrderMaLvmhUpdateMethodEnum;
import com.ruoyi.system.domain.KfnWorkOrder;
import com.ruoyi.system.domain.KfnWorkOrderMaAdidas;
import com.ruoyi.system.domain.vo.*;
import com.ruoyi.system.mapper.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.KfnWorkOrderMaLvmhBo;
import com.ruoyi.system.domain.KfnWorkOrderMaLvmh;
import com.ruoyi.system.service.IKfnWorkOrderMaLvmhService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * LVMH MA工单Service业务层处理
 *
 * @author Panda
 * @date 2024-01-05
 */
@RequiredArgsConstructor
@Service
public class KfnWorkOrderMaLvmhServiceImpl implements IKfnWorkOrderMaLvmhService {

    private final KfnWorkOrderMaLvmhMapper baseMapper;
    private final KfnWorkOrderStoreLvmhMapper kfnWorkOrderStoreLvmhMapper;
    private final KfnWorkOrderMapper kfnWorkOrderMapper;
    private final KfnProjectMapper kfnProjectMapper;

    /**
     * 查询LVMH MA工单
     */
    @Override
    public KfnWorkOrderMaLvmhVo queryById(String workOrderId){
        return baseMapper.selectVoById(workOrderId);
    }

    /**
     * 查询LVMH MA工单列表
     */
    @Override
    public TableDataInfo<KfnWorkOrderMaLvmhVo> queryPageList(KfnWorkOrderMaLvmhBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<KfnWorkOrderMaLvmh> lqw = buildQueryWrapper(bo);
        Page<KfnWorkOrderMaLvmhVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        List<KfnWorkOrderMaLvmhVo> kfnWorkOrderMaLvmhVos = result.getRecords();
        //获取所有的工单号
        List<String> workOrderIds = kfnWorkOrderMaLvmhVos.stream().map(KfnWorkOrderMaLvmhVo::getWorkOrderId).collect(Collectors.toList());
        //通过所查询的工单号差工单池内通用信息
        if(CollectionUtil.isEmpty(workOrderIds)){
            return TableDataInfo.build(result);
        }
        Map<String, KfnWorkOrderVo> kfnWorkOrderVoMap = kfnWorkOrderMapper.selectVoBatchIds(workOrderIds).stream().collect(Collectors.toMap(KfnWorkOrderVo::getWorkOrderId, kfnWorkOrderVo -> kfnWorkOrderVo));
        //将通用信息复制到独立品牌的工单上
        kfnWorkOrderMaLvmhVos.forEach(k -> {
            KfnWorkOrderVo kfnWorkOrderVo = kfnWorkOrderVoMap.get(k.getWorkOrderId());
            BeanUtil.copyProperties(kfnWorkOrderVo,k,CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        });
        result.setRecords(kfnWorkOrderMaLvmhVos);

        return TableDataInfo.build(result);
    }

    /**
     * 查询LVMH MA工单列表
     */
    @Override
    public List<KfnWorkOrderMaLvmhVo> queryList(KfnWorkOrderMaLvmhBo bo) {
        LambdaQueryWrapper<KfnWorkOrderMaLvmh> lqw = buildQueryWrapper(bo);
        List<KfnWorkOrderMaLvmhVo> kfnWorkOrderMaLvmhVos = baseMapper.selectVoList(lqw);
        //获取所有的工单号
        List<String> workOrderIds = kfnWorkOrderMaLvmhVos.stream().map(KfnWorkOrderMaLvmhVo::getWorkOrderId).collect(Collectors.toList());
        //通过所查询的工单号差工单池内通用信息
        if(CollectionUtil.isEmpty(workOrderIds)){
            return kfnWorkOrderMaLvmhVos;
        }
        Map<String, KfnWorkOrderVo> kfnWorkOrderVoMap = kfnWorkOrderMapper.selectVoBatchIds(workOrderIds).stream().collect(Collectors.toMap(KfnWorkOrderVo::getWorkOrderId, kfnWorkOrderVo -> kfnWorkOrderVo));
        //将通用信息复制到独立品牌的工单上
        kfnWorkOrderMaLvmhVos.forEach(k -> {
            KfnWorkOrderVo kfnWorkOrderVo = kfnWorkOrderVoMap.get(k.getWorkOrderId());
            BeanUtil.copyProperties(kfnWorkOrderVo,k,CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        });
        return kfnWorkOrderMaLvmhVos;
    }

    private LambdaQueryWrapper<KfnWorkOrderMaLvmh> buildQueryWrapper(KfnWorkOrderMaLvmhBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<KfnWorkOrderMaLvmh> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getResponseLevel()), KfnWorkOrderMaLvmh::getResponseLevel, bo.getResponseLevel());
        lqw.eq(StringUtils.isNotBlank(bo.getFaultClass()), KfnWorkOrderMaLvmh::getFaultClass, bo.getFaultClass());
        lqw.eq(StringUtils.isNotBlank(bo.getFaultCode()), KfnWorkOrderMaLvmh::getFaultCode, bo.getFaultCode());
        lqw.eq(StringUtils.isNotBlank(bo.getSoluteCode()), KfnWorkOrderMaLvmh::getSoluteCode, bo.getSoluteCode());
        lqw.eq(StringUtils.isNotBlank(bo.getFaultEquipment()), KfnWorkOrderMaLvmh::getFaultEquipment, bo.getFaultEquipment());
        return lqw;
    }

    /**
     * 新增LVMH MA工单
     */
    @Transactional
    @Override
    public Boolean insertByBo(KfnWorkOrderMaLvmhBo bo) {
        KfnWorkOrderMaLvmh add = BeanUtil.toBean(bo, KfnWorkOrderMaLvmh.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setWorkOrderId(add.getWorkOrderId());
        }
        //查询店铺信息
        KfnWorkOrderStoreLvmhVo kfnWorkOrderStoreLvmhVo = kfnWorkOrderStoreLvmhMapper.selectVoById(bo.getStoreId());
        BeanUtil.copyProperties(kfnWorkOrderStoreLvmhVo, bo, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        bo.setStoreAddress(kfnWorkOrderStoreLvmhVo.getProvince() + kfnWorkOrderStoreLvmhVo.getCity() + kfnWorkOrderStoreLvmhVo.getDistricts() + kfnWorkOrderStoreLvmhVo.getAddress());
        //查询项目信息
        KfnProjectVo kfnProjectVo = kfnProjectMapper.selectVoById(bo.getProjectId());
        BeanUtil.copyProperties(kfnProjectVo, bo, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));

        //写入工单池
        KfnWorkOrder kfnWorkOrder = BeanUtil.toBean(bo, KfnWorkOrder.class);
        boolean insert = kfnWorkOrderMapper.insert(kfnWorkOrder) > 0;
        if (insert && flag) {
            return true;
        } else {
            throw new RuntimeException("未知异常，回滚插入");
        }
    }
    /**
     * 修改LVMH MA工单
     */
    @Override
    public Boolean updateByBo(KfnWorkOrderMaLvmhBo bo) {
        KfnWorkOrderMaLvmh update = BeanUtil.toBean(bo, KfnWorkOrderMaLvmh.class);
        validEntityBeforeSave(update);

        if(bo.getMethodName().equals(KfnWorkOrderMaLvmhUpdateMethodEnum.UPDATE.getMethodName())) {
            //修改基础信息
            KfnWorkOrder kfnWorkOrder = BeanUtil.toBean(bo, KfnWorkOrder.class);
            kfnWorkOrderMapper.updateById(kfnWorkOrder);
        }
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(KfnWorkOrderMaLvmh entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除LVMH MA工单
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        kfnWorkOrderMapper.deleteBatchIds(ids);
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
